
#include "Gsys.h"
#include "../game/main.h"

//---------------------------------
DWORD T_SndBufferSize[C_MAX_GAME_SND_CHANNAL] = 
{
	C_Snd00ChannelsBufferSize,
	C_Snd01ChannelsBufferSize,
	C_Snd02ChannelsBufferSize,
	C_Snd03ChannelsBufferSize,
	C_Snd04ChannelsBufferSize,
	C_Snd05ChannelsBufferSize ,
};
//---------------------------------
HANDLE CGmMemAlloc::s_hRes = INVALID_HANDLE_VALUE;
LPVOID  CGmMemAlloc::s_pGmHeap = NULL;
LPVOID  CGmMemAlloc::s_pOtherBuffer = NULL;
LPVOID  CGmMemAlloc::s_pSoundBuffer[C_MAX_GAME_SND_CHANNAL];
LPVOID  CGmMemAlloc::m_pPaletteBuffer[C_MAX_PAL_NUM];
//---------------------------------
CGmMemAlloc::CGmMemAlloc()
{
	for(INT iCnt = 0; iCnt < C_MAX_PAL_NUM; ++iCnt)
	{
		m_pPaletteBuffer[iCnt] = NULL;
	}
}

CGmMemAlloc::~CGmMemAlloc()
{
	
}

void CGmMemAlloc::CreateGameMemory()
{
	CreateRes();
	AllocGameHeap();
}

void CGmMemAlloc::DestroyGameMemory()
{
	FreeGameHeap();
	DestroyRes();
}

void CGmMemAlloc::CreateRes()
{
	char ResFilePath[128];
	char *pname = (char*)GmMainPak;
	ChangePath(ResFilePath, pname);
#ifdef C_Memory_Mode_24_24
	DWORD dwResType = RES_TYPE_LOCALHEAP ;
#endif

#ifdef C_Memory_Mode_30_10
	DWORD dwResType = RES_TYPE_NORMAL ;
#endif

	s_hRes = RES_Create(RES_VERSION_100, dwResType,(LPVOID)ResFilePath, C_MainResPakDataSize, DIS_SCHEDULER);
	if((INVALID_HANDLE_VALUE== s_hRes)||(NULL== s_hRes))
	{
		PrintfString(" RES_Create error! ");
	}
}

void CGmMemAlloc::DestroyRes()
{
	HRESULT result;
	result = RES_Release(s_hRes);
	if(0 != result)
	{
		PrintfString(" Release Res error!");
	}
}

HANDLE CGmMemAlloc::GetRes()
{
	return s_hRes ;
}

LPVOID* CGmMemAlloc::GetSoundBuffer()
{	
	return s_pSoundBuffer;
}

LPVOID CGmMemAlloc::GetPalette(DWORD PalatteID)
{
	return m_pPaletteBuffer[PalatteID];
}

void CGmMemAlloc::CreateGamePallete(DWORD dwPalResID[])
{
	for( int iCnt = 0; iCnt < C_MAX_PAL_NUM; iCnt++)
	{
		CreatePalette(iCnt, dwPalResID[iCnt]) ;
	}
}

void CGmMemAlloc::DestroyGamePallete()
{
	for( int iCnt = 0; iCnt < C_MAX_PAL_NUM; iCnt++)
	{
		if(m_pPaletteBuffer[iCnt] != NULL)
		{
			LocalFree(m_pPaletteBuffer[iCnt]);
			m_pPaletteBuffer[iCnt] = NULL;
		}
	}
}


void CGmMemAlloc::AllocGameHeap()
{
	s_pGmHeap = HeapCreate(0, C_GmHeapSize, C_GmHeapSize);
	if(NULL == s_pGmHeap)
	{
		PrintfString(" HeapCreate g_pGmHeap error! ");
	}

	//jpg
	g_pJpgRegisterBuffer = HeapAlloc(s_pGmHeap, LMEM_FIXED|LMEM_ZEROINIT, C_JPGRegisterBufferSize);
	if(NULL == g_pJpgRegisterBuffer)
	{
		PrintfString(" HeapAlloc  g_pJpgRegisterBuffer error! ");
	}

	//sound
	INT iSndCnt;
	for(iSndCnt = 0; iSndCnt<C_MAX_GAME_SND_CHANNAL; ++iSndCnt)
	{
		s_pSoundBuffer[iSndCnt] = HeapAlloc(s_pGmHeap, LMEM_FIXED|LMEM_ZEROINIT, T_SndBufferSize[iSndCnt] );
		if( NULL == s_pSoundBuffer[iSndCnt] )
		{
			PrintfString(" HeapAlloc error, iSndCnt:%d ", iSndCnt);
		}
	}

	//other
	s_pOtherBuffer = HeapAlloc(s_pGmHeap,  LMEM_FIXED|LMEM_ZEROINIT, C_OtherBufferSize);
	if(NULL == s_pOtherBuffer)
	{
		PrintfString(" HeapAlloc g_pOtherBuffer error C_OtherBufferSize:%d ", C_OtherBufferSize);
	}
}

void CGmMemAlloc::FreeGameHeap()
{
	BOOL bResult ;
	bResult = HeapDestroy(s_pGmHeap);
	if(!bResult)
	{
		PrintfString(" HeapDestroy error !");
	}
}

void CGmMemAlloc::CreatePalette(DWORD dwPalID, DWORD dwPalResID)
{
	if(m_pPaletteBuffer[dwPalID] == NULL)
	{
		m_pPaletteBuffer[dwPalID] = LocalAlloc(LPTR, C_PaleteBufferSize/C_MAX_PAL_NUM);
		Gm_Assert(m_pPaletteBuffer[dwPalID] !=NULL, " ") ;
	}
	else
	{	
		memset_Ex(m_pPaletteBuffer[dwPalID], 0, C_PaleteBufferSize/C_MAX_PAL_NUM) ;
	}

	DWORD dwSize = RES_GetSize(s_hRes, dwPalResID);
	DWORD dwRealSize = 0;
	HRESULT result;
	result = RES_LoadDataToBuffer(s_hRes, 
			dwPalResID, 
			m_pPaletteBuffer[dwPalID], 
			0, 
			dwSize, 
			&dwRealSize) ;
			
	if(result != RST_SND_OK)
	{
		PrintfString("RES_LoadDataToBuffer Fail");
	}			
}
